using Digitalmes.Domain.Entities.Mes.Qualities;

namespace Digitalmes.Application.Mes.Qulities.Scraps.Queries;

/// <summary>
/// 分页查询产品报废信息命令。
/// </summary>
/// <param name="Sn">产品零件码</param>
public sealed record GetScrapInfoListWithPagedQuery(
    string? Sn,
    int PageNumber,
    int PageSize) : IPagedQuery, IRequest<IResult<PagedList<QcScrapInfoDto>>>;

internal sealed class GetScrapInfoListWithPagedQueryHandler(IRepository<QcScrapInfo> repo) : IRequestHandler<GetScrapInfoListWithPagedQuery, IResult<PagedList<QcScrapInfoDto>>>
{
    public async Task<IResult<PagedList<QcScrapInfoDto>>> Handle(GetScrapInfoListWithPagedQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.Sn), m => m.Sn == request.Sn)
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(items.Adapt<PagedList<QcScrapInfoDto>>());
    }
}
